<?php

namespace Services\Log;

use Common\Model\Log\LogModel;
use Services\BaseService;
use Think\Log;

class LogService extends BaseService
{
    /**
     * 写入文件日志
     * @param $content
     * @param $tag
     * @param $level
     * @return void
     */
    public static function write($content, $tag = '', $level = Log::INFO)
    {
        if (is_array($content)) {
            $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        }
        Log::write($tag . ' ' . $content, Log::INFO);
        return true;
    }

    /**
     * 双写日志（文件日志，数据库日志）
     * @param $content
     * @param $tag
     * @param $level
     * @return void
     */
    public static function writeWithDb($content, $tag = '', $uniqueKey = '', $level = Log::INFO)
    {
        try {
            self::write($content, $tag, $level);
            (new LogModel())->addLog($uniqueKey, $content, $tag);
            return true;
        } catch (\Exception $e) {
        }
    }
}
